//	Name: barchart.js 
//	Description:  Updates the drop-down menus at the bottom of the application and
//				  Draws the two comparison barcharts at the bottom of the application 
//	Revision History:
//  11/15/2011 Chris Lewis: Commented code for Alpha Release
//  11/13/2011 Chris Lewis: Commented the code
//  11/3/2011 Chris Lewis: Implemented barchart comparison functionality for states and counties
//  11/2/2011 Chris Lewis: Created file, wrote up csv file reading code

// - updateComparisonSelections(state)
// This function is called whenever the user changes the state being viewed, the category of data, or
// the years selected. 
// Parameters:
// 	 @state: The name of the state (or "United States") that the user has selected to view
function updateComparisonSelections(state)
{
	// Get the comparison drop-down menus from the page
	var chooseComparison1 = document.getElementById("chooseComparison1");
	var chooseComparison2 = document.getElementById("chooseComparison2");
	// Remove all of the current states or counties from the drop-down menus
	for(i=chooseComparison1.options.length-1; i >= 0;i--)
	{
		chooseComparison1.remove(i);
		chooseComparison2.remove(i);
	}
	// If the user is looking at the United States, get the states data
	if(state=="United States")
	{	
		d3.csv("StatesData.csv", function(data) {
			data.forEach(function(d)
	    	{
	    		// Add selections for all the states to the two drop-down menus
	    		var optn1 = document.createElement("OPTION");
				var optn2 = document.createElement("OPTION");
				optn1.text = d.state;
				optn2.text = d.state;
				chooseComparison1.options.add(optn1);
				chooseComparison2.options.add(optn2);
	    	});
	   	});
	 }
	 //If the user is looking at a State, get county data for the state
	 else
	 {
	 	d3.csv(state+"CountyData.csv", function(data) {
			data.forEach(function(d)
	    	{
	    		// Add selections for all the counties in the selected state to the two drop-down menus
	    		var optn1 = document.createElement("OPTION");
				var optn2 = document.createElement("OPTION");
				optn1.text = d.county;
				optn2.text = d.county;
				chooseComparison1.options.add(optn1);
				chooseComparison2.options.add(optn2);
	    	});
	   	});
	 }
	 // Draw the bar charts for the selected states/counties 
	 drawBarChart();
}

// - drawBarChart()
// This function draws the two bar charts for comparing two counties within a state or two states within the United
// States
function drawBarChart()
{
	// Get the value of the selected state
	var state = document.getElementById("chooseState");
	var options = state.options;
	state = options[state.selectedIndex].text;
	// Get the two selections in the drop-down comparison menus
	var comparison1 = document.getElementById("chooseComparison1");
	var comparison1Options = comparison1.options;
	var comparison2 = document.getElementById("chooseComparison2");
	var comparison2Options = comparison2.options;
	// If this is the first time the barcharts are loaded for a state, the comparison options
	// are empty, so set default comparisons according to which state is selected
	if(comparison1Options.length == 0) 
	{
		if(state == "United States")
		{
			comparison1 = "Alabama";
			comparison2 = comparison1;
		}
		else if(state == "California")
		{
			comparison1 = "Alameda";
			comparison2 = comparison1;
		}
		else if(state == "Hawaii")
		{
			comparison1 = "Hawaii";
			comparison2 = comparison1;
		}
		else if(state == "Illinois")
		{
			comparison1 = "Adams";
			comparison2 = comparison1;
		}
		else if(state == "Oregon")
		{
			comparison1 = "Baker";
			comparison2 = comparison1;
		}
		else if(state == "Washington")
		{
			comparison1 = "Adams";
			comparison2 = comparison1;
		}
	}
	// If the comparison options are populated, get the selected options
	else
	{
		comparison1 = comparison1Options[comparison1.selectedIndex].text; 
		comparison2 = comparison2Options[comparison2.selectedIndex].text;
	}
	// Set the csv file to open
	var dataFile = "";
	if(state == "United States")
		dataFile = "StatesData.csv";
	else
		dataFile = state+"CountyData.csv";

	// Open the appropriate csv file
	d3.csv(dataFile, function(data) {
		// These two arrays will hold the number of inquired [0], applied [1], accepted [2], and enrolled [3]
		// from the state
		var comparison1Values = [0,0,0,0];
		var comparison2Values = [0,0,0,0];
		// The csv files for states and counties are formatted differently, so we have two conditions
		if(state == "United States")
		{
			data.forEach(function(d)
			{
				// If the state of the record being iterated over is equal to either of the selections made
				// by the user, store the values seen
				if(d.state==comparison1)
				{
					// Set the inquired [0], applied [1], accepted [2], and enrolled [3]
	      			comparison1Values[0] = incrementNumber("Number Inquired", d);	 
	      			comparison1Values[1] = incrementNumber("Number Applied", d);	
	      			comparison1Values[2] = incrementNumber("Number Accepted", d);	
	      			comparison1Values[3] = incrementNumber("Number Enrolled", d);	
	      			
	  			}	
	  			if(d.state==comparison2)
	  			{
	  				// Set the inquired [0], applied [1], accepted [2], and enrolled [3]
	      			comparison2Values[0] = incrementNumber("Number Inquired", d);	 
	      			comparison2Values[1] = incrementNumber("Number Applied", d);	
	      			comparison2Values[2] = incrementNumber("Number Accepted", d);	
	      			comparison2Values[3] = incrementNumber("Number Enrolled", d);	
	  			}		
			});
		}
		// An individual state is selected, so we read county data
		else
		{
			data.forEach(function(d)
			{
				// If the state of the record being iterated over is equal to either of the selections made
				// by the user, store the values seen
				if(d.county==comparison1)
				{
					// Set the inquired [0], applied [1], accepted [2], and enrolled [3]
	      			comparison1Values[0] = incrementNumber("Number Inquired", d);	 
	      			comparison1Values[1] = incrementNumber("Number Applied", d);	
	      			comparison1Values[2] = incrementNumber("Number Accepted", d);	
	      			comparison1Values[3] = incrementNumber("Number Enrolled", d);	
	      			
	  			}	
	  			if(d.county==comparison2)
	  			{
  					// Set the inquired [0], applied [1], accepted [2], and enrolled [3]
	      			comparison2Values[0] = incrementNumber("Number Inquired", d);	 
	      			comparison2Values[1] = incrementNumber("Number Applied", d);	
	      			comparison2Values[2] = incrementNumber("Number Accepted", d);	
	      			comparison2Values[3] = incrementNumber("Number Enrolled", d);		  				
	  			}		
			});
		}
		// Set the max value seen to scale the barcharts on
		var max = 0;
		if(comparison1Values[0] > comparison2Values[0])
		{
			max = comparison1Values[0];
		}
		else
		{
			max = comparison2Values[0];
		}
		// Set the scale to be used in drawing the new bars
		var scale = d3.scale.linear()
		.domain([0, max])
  		.range(["12px", "200px"]);
  		//remove old bars on the page in order to draw new ones
  		d3.select("body")
  					.select("#comparisonSelections")
  					.select("#leftSide")
  					.select(".leftChart")
  					.remove("div")
  					.attr("class", "leftChart");
  		d3.select("body")
  					.select("#comparisonSelections")
  					.select("#rightSide")
  					.select(".rightChart")
  					.remove("div")
  					.attr("class", "rightChart");
		//Create two new bars - one for each selection - using the same
		//scale  					
  		var leftChart = d3.select("body")
					.select("#comparisonSelections")
					.select("#leftSide")
					.append("div")
					.attr("class", "leftChart");
	  
  		  leftChart.selectAll("div")
		     .data(comparison1Values)
		  	 .enter().append("div")
		     .style("width", scale)
		     .text(function(d) {return d;});
		     
		var rightChart = d3.select("body")
					.select("#comparisonSelections")
					.select("#rightSide")
					.append("div")
					.attr("class", "rightChart");
 
  		  rightChart.selectAll("div")
		     .data(comparison2Values)
		  	 .enter().append("div")
		     .style("width", scale)
		     .text(function(d) {return d;});
  		 
	});
}